//
//  NewFeatureViewController.swift
//  lixiangweibo
//
//  Created by dyy on 15/11/29.
//  Copyright © 2015年 lixianggongzuoshi. All rights reserved.
//

import UIKit
import SnapKit


private let reuseIdentifier = "Cell"
let maxCount = 4
class NewFeatureViewController: UICollectionViewController {
    
    init(){
        
        let layout = UICollectionViewFlowLayout()
        layout.itemSize = UIScreen .mainScreen().bounds.size
        layout.minimumInteritemSpacing = 0
        layout.minimumLineSpacing = 0
        layout.scrollDirection = .Horizontal
        
        super.init(collectionViewLayout: layout)
        
        collectionView?.pagingEnabled = true
        collectionView?.bounces = false
        collectionView?.showsHorizontalScrollIndicator = false
        
        
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        self.collectionView!.registerClass(NewFeatureCell.self, forCellWithReuseIdentifier: reuseIdentifier)
        
        
    }
    
    
    
    override func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        // #warning Incomplete implementation, return the number of items
        return maxCount
    }
    
    override func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCellWithReuseIdentifier(reuseIdentifier, forIndexPath: indexPath) as! NewFeatureCell
        
        cell.index = indexPath.item
        
        //cell.backgroundColor = indexPath.item % 2 == 0 ? UIColor.orangeColor() : UIColor.blueColor()
        return cell
    }
    
    
    override func collectionView(collectionView: UICollectionView, didEndDisplayingCell cell: UICollectionViewCell, forItemAtIndexPath indexPath: NSIndexPath) {
        let cell = collectionView.visibleCells().last as! NewFeatureCell
        let index = collectionView.indexPathForCell(cell)
        let indexItem = index?.item ?? 0
        if indexItem == maxCount - 1 {
        cell.startAnimation()
        
        }
        
    }
    
    
}

class NewFeatureCell: UICollectionViewCell {
    
    var index: Int = 0 {
    
    
        didSet{
        
        iconImage.image = UIImage(named: "new_feature_\(index + 1)")
            
            startBtn.hidden = true
        
        }
    
    }
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        setUpUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    
    private func setUpUI(){
        
        contentView.addSubview(iconImage)
        contentView.addSubview(startBtn)
        
        iconImage.snp_makeConstraints { (make) -> Void in
            make.edges.equalTo(contentView.snp_edges)
        }
        startBtn.snp_makeConstraints { (make) -> Void in
            make.centerX.equalTo(contentView.snp_centerX)
            make.bottom.equalTo(contentView.snp_bottom).offset(-200)
        }
        
       // print(startBtn.frame)
        
        startBtn.addTarget(self, action: "btnDidClick", forControlEvents: UIControlEvents.TouchUpInside)
        
    }
    
   @objc private func btnDidClick(){
    
    NSNotificationCenter.defaultCenter().postNotificationName(WBSwitchRootNotification, object: nil)
    }
    
    private func startAnimation(){
    
        startBtn.hidden = false
    startBtn.transform = CGAffineTransformMakeScale(0, 0)
        
        UIView.animateWithDuration(1, delay: 0, usingSpringWithDamping: 0.5, initialSpringVelocity: 5, options: [], animations: { () -> Void in
            
            self.startBtn.transform = CGAffineTransformIdentity
            
            }) { (_) -> Void in
                
                
                print("OK")
        }
    
    }
    
    
    private lazy var iconImage: UIImageView = UIImageView(image: UIImage(named: "new_feature_1"))
    private lazy var startBtn: UIButton = {
        
        let btn = UIButton()
        btn.setBackgroundImage(UIImage(named: "new_feature_finish_button"), forState:UIControlState.Normal)
        btn.setBackgroundImage(UIImage(named: "new_feature_finish_button_highlighted"), forState:UIControlState.Highlighted)
        btn.setTitle("开始体验", forState: UIControlState.Normal)
        
        btn.sizeToFit()
        return btn
        }()
    
}
